package org.example.pattern.factory.config;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.setting.dialect.Props;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author zhangjg
 * @Description
 * @Date 2021/9/18 上午10:38
 **/
public class FactoryMethod implements IFactoryMethod {

    private static final Log logger = LogFactory.get();
    private static final Map<Integer, Fruit> fruitMap = new HashMap<>();

    static {
        Fruit fruit = null;
        Props prop = Props.getProp("bean.properties");
        Set<Object> sets = prop.keySet();
        try {
            for (Object o : sets) {
                Integer key = Integer.parseInt(o.toString());
                fruit = (Fruit)Class.forName(prop.getStr(o.toString())).newInstance();
                fruitMap.put(key, fruit);
                System.out.println(o + "|" + prop.getStr(o.toString()));
            }
        } catch (Exception e) {
            logger.error(e, "ERROR load bean.properties");
        }

    }

    @Override
    public Fruit getFruit(int type) {

        Fruit fruit = fruitMap.get(type);

        return fruit;
    }

}
